package org.dromara.system.service.impl;

import lombok.extern.slf4j.Slf4j;
import cn.hutool.core.collection.CollUtil;
import org.dromara.common.core.utils.StringUtils;
import org.dromara.common.core.utils.MapstructUtils;
import org.dromara.common.mybatis.binding.Binder;
import org.dromara.common.mybatis.core.page.TableDataInfo;
import org.dromara.common.mybatis.core.page.PageQuery;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import lombok.RequiredArgsConstructor;
import org.springframework.stereotype.Service;
import org.dromara.system.domain.bo.SysBillRuleBo;
import org.dromara.system.domain.vo.SysBillRuleVo;
import org.dromara.system.domain.SysBillRule;
import org.dromara.system.mapper.SysBillRuleMapper;
import org.dromara.system.service.ISysBillRuleService;
import org.springframework.transaction.annotation.Transactional;
import org.dromara.common.mybatis.service.impl.ServiceImpl;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;

import java.util.function.Function;
import java.util.ArrayList;
import java.util.List;
import java.util.Map;
import java.util.Collection;

/**
 * 单据规则Service业务层处理
 *
 * @author yilong.fu
 * @date 2024-11-06
 */
@RequiredArgsConstructor
@Service
@Slf4j
public class SysBillRuleServiceImpl extends ServiceImpl<SysBillRuleMapper, SysBillRule, SysBillRuleBo, SysBillRuleVo> implements ISysBillRuleService {


    public SysBillRuleVo fill(SysBillRuleVo entity) {
        SysBillRuleVo vo = Binder.convertAndBindRelations(entity, SysBillRuleVo.class);
        return vo;
    }

    public LambdaQueryWrapper<SysBillRule> buildQueryWrapper(SysBillRuleBo bo) {
        LambdaQueryWrapper<SysBillRule> lqw = Wrappers.lambdaQuery();
        lqw.like(StringUtils.isNotBlank(bo.getName()), SysBillRule::getName, bo.getName());
        lqw.eq(StringUtils.isNotBlank(bo.getCategory()), SysBillRule::getCategory, bo.getCategory());
        lqw.eq(StringUtils.isNotBlank(bo.getEnCode()), SysBillRule::getEnCode, bo.getEnCode());
        lqw.eq(StringUtils.isNotBlank(bo.getPrefix()), SysBillRule::getPrefix, bo.getPrefix());
        lqw.eq(StringUtils.isNotBlank(bo.getDateFormat()), SysBillRule::getDateFormat, bo.getDateFormat());
        lqw.eq(bo.getDigit() != null, SysBillRule::getDigit, bo.getDigit());
        lqw.eq(StringUtils.isNotBlank(bo.getStartNumber()), SysBillRule::getStartNumber, bo.getStartNumber());
        lqw.eq(StringUtils.isNotBlank(bo.getExample()), SysBillRule::getExample, bo.getExample());
        lqw.eq(bo.getThisNumber() != null, SysBillRule::getThisNumber, bo.getThisNumber());
        lqw.eq(StringUtils.isNotBlank(bo.getOutputNumber()), SysBillRule::getOutputNumber, bo.getOutputNumber());
        lqw.eq(StringUtils.isNotBlank(bo.getDescription()), SysBillRule::getDescription, bo.getDescription());
        lqw.eq(bo.getSeq() != null, SysBillRule::getSeq, bo.getSeq());
        return lqw;
    }


    /**
     * 更新前的数据校验
     */
    @Transactional(rollbackFor = Exception.class)
    public boolean validEntityBeforeUpdate(SysBillRule entity) {
        //TODO 做一些数据校验,如唯一约束
        return true;
    }

    /**
     * 保存前的数据校验
     */
    @Transactional(rollbackFor = Exception.class)
    public boolean validEntityBeforeSave(SysBillRule entity) {
        //TODO 做一些数据校验,如唯一约束
        return true;
    }


}
